//
//  BuyBackPasswordViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "BuyBackPasswordViewController.h"
#import "RSA.h"

static int seconds = 60;//计时60s
@interface BuyBackPasswordViewController ()<UITextFieldDelegate>
{
    UIView *backView;
    UITextField *passwordField;
    UITextField *sourePassField;
    UITextField *codeField;
    UIButton *codeBtn;
    NSTimer *timer;
}


@end

@implementation BuyBackPasswordViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpanhuishou:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    //    [timer invalidate];
    [self renewTimer];
    timer = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    
}

- (void)creatUI
{
    self.view.backgroundColor = BacksColor;
    self.navigationItem.title = @"设置提现密码";
    self.navigationController.navigationBar.translucent = NO;
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150*displaywidth)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    NSArray *titleAry = @[@"提现密码",@"确认密码",@"验证码"];
    NSArray *pladerAry = @[@"请输入6位数字提现密码",@"请再次输入提现密码",@"请输入验证码"];
    for (int i = 0; i < titleAry.count; i ++)
    {
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth+50*displaywidth*i, 80*displaywidth, 40*displaywidth) Font:15.0 Text:titleAry[i] Color:RGB(77, 77, 77)];
        [backView addSubview:titLab];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(110*displaywidth, 5*displaywidth+50*displaywidth*i, SCREEN_WIDTH-130*displaywidth, 40*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:15.0];
        [textfield setValue:RGB(153, 153, 153) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = RGB(77, 77, 77);
        textfield.delegate = self;
        textfield.keyboardType = UIKeyboardTypeNumberPad;
        [backView addSubview:textfield];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 49*displaywidth+50*displaywidth*i, SCREEN_WIDTH-15*displaywidth, 1*displaywidth)];
        line.backgroundColor = LinesColor;
        [backView addSubview:line];
       
        if (i == 0)
        {
            textfield.secureTextEntry = YES;
            passwordField = textfield;
        }
        else if (i == 1)
        {
            textfield.secureTextEntry = YES;
            sourePassField = textfield;
        }
        else
        {
            textfield.frame = CGRectMake(110*displaywidth, 5*displaywidth+50*displaywidth*i, 120*displaywidth, 40*displaywidth);
            codeField = textfield;
            codeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-110*displaywidth, 107.5*displaywidth, 100*displaywidth, 35*displaywidth) ImageName:nil Target:self Action:@selector(verifiBtnAction:) Title:@"获取验证码" Font:13.0];
            [codeBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
            codeBtn.backgroundColor = [UIColor whiteColor];
            [codeBtn.layer setCornerRadius:3*displaywidth];
            [codeBtn.layer setMasksToBounds:YES];
            codeBtn.layer.borderColor = ThemeColor.CGColor;
            codeBtn.layer.borderWidth = 1*displaywidth;
            [backView addSubview:codeBtn];
        }
        
    }
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 290*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureAction) Title:@"确定" Font:15.0];
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.backgroundColor = ThemeColor;
    [soureBtn.layer setMasksToBounds:YES];
    [soureBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:soureBtn];
    
    
}


- (void)verifiBtnAction:(UIButton *)btn
{
    [self requestRegisCode4register];
}

//验证码
- (void)requestRegisCode4register
{
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":[LTools cacheForKey:SERVER_LoginPhone]};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestGetUpdateWithdrawPswCodeParmers:dic Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"验证码已发送" addToView:weakSelf.view];
             [weakSelf startTimer];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}


//确认
- (void)soureAction
{
    [self.view endEditing:YES];
    if ([passwordField.text length] != 6)
    {
        [LTools showMBProgressWithText:@"请设置6位数字密码" addToView:self.view];
        return;
    }
    if (![passwordField.text isEqualToString:sourePassField.text])
    {
        [LTools showMBProgressWithText:@"两次密码不一致" addToView:self.view];
        return;
    }
    if ([codeField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入验证码" addToView:self.view];
        return;
    }
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestPublicKeyParmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             NSString *publicKey = obj[@"data"];
             //测试要加密的数据
             NSString *sourceStr = passwordField.text;
             //公钥加密
             NSString *encryptStr = [RSA encryptString:sourceStr publicKey:publicKey];
             NSDictionary *dic = @{@"phone":[LTools cacheForKey:SERVER_LoginPhone],
                                   @"withdrawPassword":encryptStr,
                                   @"code":codeField.text};
             [weakSelf requestRegisterParmers:dic];
             
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
             [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
     }];
   
}

- (void)requestRegisterParmers:(NSDictionary *)dic
{
    WEAKSELF;
    [HYBaseHandler requestupdateWithdrawPswParmers:dic Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];

         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"操作成功" addToView:sharedDelegate.window];
             [weakSelf.navigationController popViewControllerAnimated:YES];
             
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
         
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];

     }];
}

- (void)startTimer
{
    seconds = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateTime) userInfo:Nil repeats:YES];
    codeBtn.userInteractionEnabled = NO;
}
//计算时间
- (void)calculateTime
{
    NSString *title = [NSString stringWithFormat:@"%dS",seconds];
    codeBtn.titleLabel.text = title;
    [codeBtn setTitle:title forState:UIControlStateNormal];
    
    if (seconds != 0) {
        seconds --;
    }else
    {
        [self renewTimer];
    }
    
}
//计时器归零
- (void)renewTimer
{
    [timer invalidate];//计时器停止
    codeBtn.userInteractionEnabled = YES;
    [codeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    seconds = 60;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//    if (textField == codeField)
//    {
//        backView.frame = CGRectMake(0, -70*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
//
//    }
}

- (void)scrollTap:(id)sender
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [self.view endEditing:YES];
}

- (void)jianpanhuishou:(NSNotification *)notification
{
//    backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
